<?php

namespace Home\Controller;

use Think\Controller;

class NewsController extends Controller {
    private $categorys;
    private $field;
    private $show_thumb;
    //初始化
    public function _initialize(){
        //获取栏目信息
        if(!$this->categorys = F('category_content')){
            D('Admin/ArticleCategory')->file_cache();
            $this->categorys = F('category_content');
        }
        //是否显示缩略图
        
        if(I('get.thumb') === 'true' ){
            $this->show_thumb = 1;
        }elseif(I('get.thumb') === 'false'){
            $this->show_thumb = 0;
        }else{
            //如果不存在cookie.thumb, 默认显示图片, 并设置
            if(cookie('thumb') !== "0"){
                $this->show_thumb = 1;
            }else{
                $this->show_thumb = 0;
            }
        }
        if($this->show_thumb != cookie('thumb'))
            cookie('thumb', (string)$this->show_thumb);

        if($this->show_thumb){
            $this->field = 'id, title, catid, inputtime, thumb';
        }else{
            $this->field = 'id, title, catid, inputtime';
        }
        $this->assign('thumb', $this->show_thumb);
    }
    
    public function index(){
        //列出最新的文章
        //$offset = I('post.offset', '0', 'intval');
        if(I('post.id')){
            $map['id'] = array('lt', I('post.id'));
        }
        
        $pagesize = 20;
        
        $lists = M('Article')->where($map)->order('id DESC')->field($this->field)->limit(0, $pagesize)->select();
        if($lists){
            foreach ($lists as $key => $article){
                $lists[$key]['catname'] = $this->categorys[$article['catid']]['catname'];
                $lists[$key]['create_time'] = date('m-d H:i', $article['inputtime']);
            }
        }
        if(IS_POST){
            if($lists){
                $return['flag'] = true;
                $return['data'] = $lists;
        
                //是否还可以加载
                $next = M('Article')->where($map)->order('id')->field('id')->limit($pagesize + 1, 1)->select();
                $return['next'] = $next ? true : false;
            }else{
                $return['flag'] = false;
            }
        
            $this->ajaxReturn($return);
        }else{
            $this->assign('page_list', $lists);
            $category = $this->categorys;
            $this->assign('categorys', $this->categorys);
            $this->assign('title', '最新文章');
            $this->display();
        }
    }
    
    public function lists(){
        
        $catid = I('catid', '', 'intval');
        //$offset = I('post.offset', '0', 'intval');
        if(I('post.id')){
            $map['id'] = array('lt', I('post.id'));
        }
        $pagesize = 20;
        $map['catid'] = $catid;
        
        $lists = M('Article')->where($map)->order('id DESC')->field($this->field)->limit(0, $pagesize)->select();

        if($lists){
            foreach ($lists as $key => $article){
                $lists[$key]['create_time'] = date('m-d H:i', $article['inputtime']);
            }
            //是否还可以加载
            $next = M('Article')->where($map)->order('id')->field('id')->limit($pagesize, 1)->select();
        }

        
        if(IS_POST){
            if($lists){
                $return['flag'] = true;
                $return['data'] = $lists;
                
                $return['next'] = $next ? true : false; 
            }else{
                $return['flag'] = false;
            }
            
            $this->ajaxReturn($return);
        }else{
            $this->assign('next', $next);
            $this->assign('page_list', $lists);
            $this->assign('catid', $catid);
            $this->assign('catname', $this->categorys[$catid]['catname']);

            $this->assign('title', $this->categorys[$catid]['catname']);
            $this->assign('categorys', $this->categorys);
            $this->display();
        }
        
    }
    
    public function show(){
        $id = I('get.id', '', 'intval');
        $map['id'] = $id;
        $detail = D('Admin/Article')->getDetail($id);
        if(!$detail){
            $this->error('无此文章');
        }
        if(!$this->show_thumb){
            $detail['content'] = preg_replace("/<img.+?>/", "", $detail['content']);
        }else{
            //查看图片
            $ext = 'gif|jpg|jpeg|bmp|png';
            preg_match_all("/(href|src)=([\"|']?)([^ \"'>]+\.($ext)(\!web)?)\\2/i", $detail['content'], $matches);
            foreach ($matches[3] as $_key => $pic){
                $oldpath[] = $pic;
                //$newpath[] = '/index.php?m=Home&c=News&a=picture&url='.$pic;
                $newpath[] = 'http://img.pdosgk.com/pic.php?url='.$pic;
            }
            $detail['content'] =  str_replace($oldpath, $newpath, $detail['content']);
        }
        //如果是爬虫, 记录爬虫次数
        if(isSpider()){
            $update_data['spider_count'] = array('exp', 'spider_count+1');
        }else{
            $update_data['views'] = array('exp', 'views+1');
        }
        D('Admin/Article')->where(array('id' => $id))->save($update_data);
        
        $this->assign('detail', $detail);
        $this->assign('catname', $this->categorys[$detail['catid']]['catname']);

        $this->assign('title', $detail['title']);
        $this->assign('spider_list', $this->getSpiderList());
        $this->assign('categorys', $this->categorys);
        $this->display();
    }
    
    public function getSpiderList(){
        return D('Admin/Article')->order('spider_count, views')->limit(5)->select();
    }
    
    public function picture(){
        $url = I('get.url');
        $snoopy = new \Lain\Snoopy;
        //$snoopy->referer = 'http://www.tuicool.com/';

        //$snoopy->rawheaders["COOKIE"] = "_tuicool_session=BAh7CUkiD3Nlc3Npb25faWQGOgZFRkkiJWViOTQ5NWE0NjJmNzFlYTdjZmJjZWU1ODRkMzBhNTk2BjsAVEkiDHVzZXJfaWQGOwBGaQNdygFJIhBfY3NyZl90b2tlbgY7AEZJIjFLU3ZPLzQ4Mk5RQk1Ndld5YnUrK2JuTEVhK0hNR3EyQUdtUVhBcU1LblU0PQY7AEZJIg5yZXR1cm5fdG8GOwBGSSIsaHR0cDovL3d3dy50dWljb29sLmNvbS9hcnRpY2xlcy9ySVJubWVxBjsARg%3D%3D--0169cd82b559a1b6d9ad5b9d24b7ee3c8fc6204f; domain=.tuicool.com; path=/; expires=Wed, 06-Dec-2017 05:46:41 GMT; HttpOnly";
        //代理
        //$snoopy->proxy_host = S('proxy_host');
        //$snoopy->proxy_port = S('proxy_port');
        $snoopy->fetch($url);
        $html_code = $snoopy->results;
        //var_dump($html_code);exit;
        header('Content-type: image/jpeg; charset=utf-8');
        echo $html_code;
    }
    
    public function search(){
        
        $keyword = I('keyword');
        $offset = I('post.offset', '0', 'intval');
        $map['title'] = array('like', "%$keyword%");
        $pagesize = 20;
        
        $lists = M('Article')->where($map)->order('id DESC')->field($this->field)->limit($offset, $pagesize)->select();

        if($lists){
            foreach ($lists as $key => $article){
                $lists[$key]['create_time'] = date('m-d H:i', $article['inputtime']);
            }

            //是否还可以加载
            $next = M('Article')->where($map)->order('id')->field('id')->limit($pagesize+$offset, 1)->select();
        }
        if(IS_POST){
            if($lists){
                $return['flag'] = true;
                $return['data'] = $lists;
        
                $return['next'] = $next ? true : false;
            }else{
                $return['flag'] = false;
            }
        
            $this->ajaxReturn($return);
        }else{
            $this->assign('next', $next);
            $this->assign('page_list', $lists);
            $this->assign('keyword', $keyword);
            $this->display();
        }
    }
}

?>